
#include	"SysConfig.h"
#include	"Define.h"
#include	"Port.h"
#include	"Model_Option.h"
#include	"Boot_FlashDrv.h"
#ifdef  AV_CONTROLLER
#include	"AVController.h"
#endif

#pragma SECTION rom BootVER_E_SEC
unsigned char const BootVER_E[] = {
	'v', 'e', 'r','0','0','1'
};
#pragma SECTION rom BootVER_SEC
unsigned char const BootVER[] = {
	'v', 'e', 'r','0','0','1'
};

typedef struct{
	unsigned char ucValid;
	unsigned char ucLocal;
}ROM_OptionMemory;

extern unsigned short usBOOT_FlashData[3];

#ifdef  M16C_30PG

#define ROM_BLOCK_OPTION_MEMORY     (0xC0010)
#define ROM_BLOCK_OPTION_MEMORY_END ((unsigned far *)0xCFFFE)

#endif
#ifdef  M16C_64G

#define ROM_BLOCK_OPTION_MEMORY     (0x80010)
#define ROM_BLOCK_OPTION_MEMORY_END ((unsigned far *)0x8FFFE)

#endif


unsigned char ucUI_Option;
UI_LOCAL ucUI_OPT_Local;

#ifdef	BLUETOOTH
unsigned char ucOption_Bluetooth;
#endif

#define	BACKUP_ROM_SIZE		64

#define	BACKUP_DUMMY		0
#define	BACKUP_LOCAL		1
#define	BACKUP_LOCAL_I		2

#define	OPT_IN		(p5 >> 3) & 0x1B//(p5 & 0xD8) >> 3

static union {
	unsigned char	ub[BACKUP_ROM_SIZE];
	unsigned short	 uw[ (BACKUP_ROM_SIZE / sizeof (unsigned short)) ];
}ucROM_Data;

void OPT_ReadOptionMemory(void)
{	
	unsigned short far *usAddress;

	usAddress = (unsigned short far *)ROM_BLOCK_OPTION_MEMORY;
	memcpy(&ucROM_Data.ub[0],usAddress,BACKUP_ROM_SIZE);

	if  (ucROM_Data.ub[BACKUP_LOCAL] == (~ucROM_Data.ub[BACKUP_LOCAL_I]))
	{
		ucUI_OPT_Local = ucROM_Data.ub[BACKUP_LOCAL];
	}
}

void OPT_WriteOptionMemory(void)
{
	ucROM_Data.ub[0] = 0x00;
	ucROM_Data.ub[1] = ucUI_OPT_Local;
	ucROM_Data.ub[2] = ~ucUI_OPT_Local;
	
	asm("fclr I");
	BOOT_BlockErase(ROM_BLOCK_OPTION_MEMORY_END);
	BOOT_FlashWrite((unsigned short far *)ROM_BLOCK_OPTION_MEMORY,&ucROM_Data.uw[0],BACKUP_ROM_SIZE);
	usBOOT_FlashData[0] = 0x6576;
	usBOOT_FlashData[1] = 0x3072;
	usBOOT_FlashData[2] = 0x3130;
#ifdef  M16C_30PG
	BOOT_FlashWrite((unsigned short far *)0xC0000, &usBOOT_FlashData[0], 6);
#else
	BOOT_FlashWrite((unsigned short far *)0x80000, &usBOOT_FlashData[0], 6);
#endif
	asm("fset I");
}

unsigned char OPT_GetOption(unsigned char ucOpt)
{
	if  (ucUI_Option & ucOpt)
	{
		return(1);
	}
	
	return(0);
}

void OPT_SetOption(unsigned char ucOpt)
{
	ucUI_Option = ucUI_Option | ucOpt;
}

void OPT_ClearOption(unsigned char ucOpt)
{
	ucUI_Option = ucUI_Option & (~ucOpt);
}

#ifdef	BLUETOOTH

void OPT_ReadBluetoothOption(void)
{
	unsigned char i;
	unsigned char Cnt = 0;

	PO_OPT_BT = 1;
	for(i = 0; i < 10; ++i)
	{		
	}
	if  (PI_KEY4 == 1)
	{
		++Cnt;
	}
	
	PO_OPT_BT = 0;
	for(i = 0; i < 10; ++i)
	{		
	}
	if  (PI_KEY4 == 0)
	{
		++Cnt;
	}

	if  (Cnt == 2)
	{
		OPT_SetOption(OPT_MASK_BT);
	}
	else
	{	
		OPT_ClearOption(OPT_MASK_BT);
	}
}
#endif

#ifdef  MODEL_ARA5040
void OPT_Read_FaderOption(void)
{	
	unsigned char i;
	unsigned char Cnt = 0;

	PO_OPT = 1;
	for(i = 0; i < 10; ++i)
	{		
	}
	if  (PI_KEY1 == 1)
	{
		++Cnt;
	}
	
	PO_OPT = 0;
	for(i = 0; i < 10; ++i)
	{		
	}
	if  (PI_KEY1 == 0)
	{
		++Cnt;
	}

	if  (Cnt == 2)
	{
		OPT_SetOption(OPT_MASK_FAD);
	}
	else
	{
		OPT_ClearOption(OPT_MASK_FAD);
	}
}
#endif

#ifdef  MODEL_AGC505X
void OPT_ReadOption(void)
{	
	unsigned char i;
	unsigned char Cnt = 0;
	unsigned char ucOPT1;
	unsigned char ucOPT2;

	PD_OPT1 = 1;		// OPT1
	PD_OPT2 = 0;		// OPT2
	PO_OPT1 = 1;

	for(i = 0; i < 10; ++i)
	{
		M_NOP;
	}
	ucOPT1 = OPT_IN;
	PO_OPT1 = 0;
	for(i = 0; i < 10; ++i)
	{
		M_NOP;
	}
	if  (OPT_IN != 0x00)
	{
		ucOPT1 = 0;
	}

	PD_OPT1 = 0;		// OPT1
	PD_OPT2 = 1;		// OPT2
	PO_OPT2 = 1;

	for(i = 0; i < 10; ++i)
	{
		M_NOP;
	}
	ucOPT2 = OPT_IN;
	PO_OPT2 = 0;
	for(i = 0; i < 10; ++i)
	{
		M_NOP;
	}
	if  (OPT_IN != 0x00)
	{
		ucOPT2 = 0;
	}

	if  (ucOPT1 & 0x01)
	{
		OPT_ClearOption(OPT_MASK_CD);
	}
	else
	{
		OPT_SetOption(OPT_MASK_CD);
	}
	if  (ucOPT1 & 0x02)
	{
		OPT_SetOption(OPT_MASK_AVC);
	}
	else
	{
		OPT_ClearOption(OPT_MASK_AVC);
	}
	
	if  (ucOPT1 & 0x08)
	{
		OPT_ClearOption(OPT_MASK_AG);
	}
	else
	{
		OPT_SetOption(OPT_MASK_AG);
	}
	if  (ucOPT1 & 0x10)
	{
		OPT_ClearOption(OPT_MASK_LOGO);
	}
	else
	{
		OPT_SetOption(OPT_MASK_LOGO);
	}
	
	if  (ucOPT2 & 0x01)
	{
		OPT_SetOption(OPT_MASK_BT);
	}
	else
	{
		OPT_ClearOption(OPT_MASK_BT);
	}
			
	if  (ucOPT2 & 0x02)
	{
		OPT_SetOption(OPT_MASK_CLOCK);
	}
	else
	{
		OPT_ClearOption(OPT_MASK_CLOCK);
	}
//////////////////////////////////////////////////////////////////////
//	OPT_ClearOption(OPT_MASK_AVC);
//	OPT_SetOption(OPT_MASK_CD);
//	OPT_SetOption(OPT_MASK_CLOCK);
//	OPT_ClearOption(OPT_MASK_LOGO);
//	OPT_ClearOption(OPT_MASK_AG);
//	OPT_ClearOption(OPT_MASK_BT);
//////////////////////////////////////////////////////////////////////
}
#endif

void Option_Read(void)
{
#ifdef  MODEL_AGC505X
	OPT_ReadOption();
#else
#if  defined(BLUETOOTH) && defined(MODEL_AGC5030)
	OPT_ReadBluetoothOption();
#endif
#ifdef  MODEL_ARA5040
	OPT_Read_FaderOption();
#endif
#endif
}


